package org.csu.jpetstore.controller;

import org.csu.jpetstore.domain.Account;
import org.csu.jpetstore.domain.Category;
import org.csu.jpetstore.domain.Product;
import org.csu.jpetstore.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/account")
public class AccountController {
    @Autowired
    private AccountService accountService;

    @RequestMapping("/view")
    public String view(Account account,Model model) {
        List<Account> query = accountService.query(account);
        model.addAttribute("query",query);
        return "account/account";
    }

    @RequestMapping("/toadd")
    public String toadd() {
        return "account/add";
    }

    @RequestMapping("/add")
    public String add(Account account) {
        accountService.insert(account);
        return "redirect:/account/view";
    }

    @RequestMapping("/delete")
    public String delete(String userid) {
        accountService.delete(userid);
        return "redirect:/account/view";
    }
    @RequestMapping("/update")
    public String update(Account account) {
        accountService.update(account);
        return "redirect:/account/view";
    }
    @RequestMapping("/get")
    public String get(String userid,Model model) {
        Account byId = accountService.getById(userid);
        model.addAttribute("user",byId);
        return "account/edit";
    }
    @GetMapping("/register")
    public  String register(){
        return "register";
    }
}
